/**
 * 
 */
package com.kathez.dao;

import java.util.List;

import com.kathez.entity.BusinessOperable;
import com.kathez.entity.KathezEntity;
import com.kathez.exceptions.KathezRuntimeException;

/**
 * @author Balaji T
 *
 */
public final class DAOFetchResultImpl extends AbstractDAOFetchResult {

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#getFetchCount()
	 */
	@Override
	public int getFetchCount() {
		return this.fetchCount;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#getFetchErrCode()
	 */
	@Override
	public String getFetchErrCode() {
		return this.fetchErrCode;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#getFetchErrMsg()
	 */
	@Override
	public String getFetchErrMsg() {
		return this.fetchErrMsg;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#getFetchedObject()
	 */
	@Override
	public BusinessOperable getFetchedObject() {
		return this.fetchedObject;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#getFetchedObjectList()
	 */
	@Override
	public List<KathezEntity> getFetchedObjectList() {
		return this.fetchedObjectList;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOFetchResult#isFetchSuccess()
	 */
	@Override
	public boolean isFetchSuccess() {
		return this.fetchSuccess;
	}

	private int fetchCount;
	private String fetchErrCode;
	private String fetchErrMsg;
	private BusinessOperable fetchedObject;
	private List<KathezEntity> fetchedObjectList;
	private boolean fetchSuccess;
	private boolean overrideSuccesFlag;
	/**
	 * @param fetchCount the fetchCount to set
	 */
	private void setFetchCount(int fetchCount) {
		this.fetchCount = fetchCount;
	}

	/**
	 * @param fetchErrCode the fetchErrCode to set
	 */
	public void setFetchErrCode(String fetchErrCode) {
		this.fetchErrCode = fetchErrCode;
	}

	/**
	 * @param fetchErrMsg the fetchErrMsg to set
	 */
	public void setFetchErrMsg(String fetchErrMsg) {
		this.fetchErrMsg = fetchErrMsg;
	}

	/**
	 * @param fetchedObject the fetchedObject to set
	 */
	public void setFetchedObject(BusinessOperable fetchedObject) {
		if(fetchedObject != null){
			if(this.fetchedObjectList != null)
				throw new KathezRuntimeException("The Result already set for this object. The existing result is List of Fetched objects");
			this.fetchedObject = fetchedObject;
			setFetchCount(1);
			if(overrideSuccesFlag && !this.fetchSuccess)
				this.fetchSuccess = false;
			else
				this.fetchSuccess = true;
		}
		this.overrideSuccesFlag = false;
	}

	/**
	 * @param fetchedObjectList the fetchedObjectList to set
	 */
	public void setFetchedObjectList(List<KathezEntity> fetchedObjectList) {
		if(fetchedObjectList != null){
			if(this.fetchedObject != null)
				throw new KathezRuntimeException("The Result already set for this object. The existing result is Single Fetched object");
			this.fetchedObjectList = fetchedObjectList;
			setFetchCount(fetchedObjectList.size());
			if(overrideSuccesFlag && !this.fetchSuccess)
				this.fetchSuccess = false;
			else
				this.fetchSuccess = true;
		}
		this.overrideSuccesFlag = false;
	}

	/**
	 * @param fetchSuccess the fetchSuccess to set
	 */
	public void setFetchSuccess(boolean fetchSuccess) {
		this.overrideSuccesFlag = true;
		this.fetchSuccess = fetchSuccess;
	}
		
}
